package components.game
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_close;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import classes.clt_events;
	
	import components.base.uif_source;
	import components.clt_component;
	import components.pj_hscrollArea;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_myVictory extends clt_component {
		protected 	var _itemsLayer: pj_hscrollArea;
		private 	var _filterList: Array;
		
		public function clt_myVictory(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0) {
			super(parent, ax, ay, awidth, aheight, 'tblock');
			var inner: Rectangle = getInner();
			_itemsLayer = new pj_hscrollArea(this, inner.x, inner.y, inner.width, inner.height, 'items', 27, false);
			_itemsLayer.addEvent(Event.CHANGE, doChange);
			new uif_close(this, width - 15, - 5);
			addEvent(Event.CLOSE, doClose);
		}
		
		protected function doClose(e: Event): void {
			slideHide(function(): void {
				dispose();
			});
		}
		
		public function assignList(list: Array): void {
			var tmp: Object = _itemsLayer.selectItem?_itemsLayer.selectItem.object:null;
			var item_width: int = _itemsLayer.calcFrameWidth(1);
			_filterList = list.concat([]);
			while (_filterList.length < (Math.floor(_itemsLayer.displayRect.width / item_width))) _filterList.push(null);
			_itemsLayer.assignList(_filterList, item_width, createItemFunc, 1);
		} 

		protected function createItemFunc(_scroll: uif_component, inner: Rectangle, index: int): uif_source {
			var ctrl: uif_source = new uif_source(_scroll, inner.x, inner.y, inner.width, inner.height, 'userPhoto');
			var vic: Object = _filterList[index];
			if (vic) {
				ctrl.loadSource(clt_servers.IMAGE_URL + 'k01.png');
				ctrl.hint = Utils.sprintf(clt.classes.locale.VICTORYHINT, [Utils.mySQLTime(vic.time), vic.rate]);
				ctrl.buttonMode = true;
			}
			return ctrl;
		}
		
		protected function doChange(e: Event): void {
			if (_filterList[_itemsLayer.selectIndex]) {
				clt.workspace.dispatchEvent(new ObjectEvent(clt_events.SHOWGAMEITEM,
					uif_util.union(_filterList[_itemsLayer.selectIndex], clt.user)));
			} 
		}
	}
}